#pragma once
#include <qdialog>
#include "ModbusPoll.h"

#include "qmodbusdevice_global.h"

class QTableWidget;
class QMODBUSDEVICE_EXPORT ModbusDlg :
    public QDialog
{
    Q_OBJECT
public:
    ModbusDlg(QWidget* parent = nullptr);

    void initUI(ModbusPoll *mb);
    void setModbusPtr(ModbusPoll* mb) { mbPoll_ = mb; }
    void updateSettings();

    /*void updateSerialSettings(QString name, int baud, char parity, int dataBit, int stopBit);
    void updateTcpSettings(QString ip, int port);*/

protected:
    QWidget* initSerialSettings();
    QWidget* initTcpSettings();
    QWidget* initRegisterDataCtrl();

    // 根据功能码初始化数据
    void getTableData(int funCode, QTableWidget *table);
    void fillData(int funCode, QTableWidget* table);

    // 读写modbus
    void readAndWrite(int funCode, int addr, int n, QTableWidget* table);

    //void saveModbusSet();

private slots:

    void on_saveModbusSet();

private:
    std::function<void()> updateSerialSettigns_; 
    std::function<void()> updateTcpSettigns_;
    ModbusSettings settings_;
    ModbusPoll* mbPoll_ = nullptr;


    uint16_t regDatas[30];
    uint8_t dataBits[30];
};

